package history;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;

public class CreatePublicHistory {

	private PreparedStatement stmt;
	static Connection conn = null;
	
	
	static public boolean connectionToDataBase (){

        try
        {             
            String dbName = "MultiChat";
            String url = "jdbc:derby:" + dbName + ";create=true";
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance ();
            conn = DriverManager.getConnection(url);
            return true;
        }catch (Exception e){
           	System.err.println ("Cannot connect to database server");
            e.printStackTrace();
            return false;
        }  
     }
	
	public boolean addToHistory(String room, String data, String sender, String receiver, String message){
		
		try {
			String insert = "INSERT INTO History (room, date, sender, receiver, message) VALUES (?, ?, ?, ?, ?)";
			stmt = conn.prepareStatement(insert);
			stmt.setString(1, room);
			stmt.setString(2, data);
                        stmt.setString(3, sender);
                        stmt.setString(4, receiver);
			stmt.setString(5, message);

			stmt.executeUpdate();
                        return true;
		} catch (SQLException e) {
			e.printStackTrace();
                        return false;
		}
	}
	
	
}
